package com.A21MobileSales;

import java.util.ArrayList;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.A21.Adapter.FrequentListAdapter;
import com.A21.Object.FrequentPurchaseList;
import com.A21MobileSales.DataBase.DatabaseHelper;

public class FrequentProduct extends Activity {

	public static final int PRODUCT_DETAILS_ACTIVITY = 1;
	public static final int PRODUCT_SEARCH_ACTIVITY = 2;
	DatabaseHelper ObjDBHelper;
	ArrayList<FrequentPurchaseList> lstProducts;
	Button btnDelSearch;
	LinearLayout btnSearhAdvanced;
	EditText edtSearchProduct;
	ListView lvProduct;
	FrequentListAdapter productListAdapter;
	private String CustCode, CName, strId, TrnType;
	String Brand, Model, Made, SearchText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_selection);
		getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		try {
			ObjDBHelper = new DatabaseHelper(this);
			// lstProducts = ObjDBHelper.GetAllProducts();
			lvProduct = (ListView) findViewById(R.id.OLS_lvOrderLineList);
			lvProduct.setDivider(null);

			if (getIntent().hasExtra("Code")) {
				CustCode = getIntent().getStringExtra("Code");
			}
			if (getIntent().hasExtra("CName")) {
				CName = getIntent().getStringExtra("CName");
			}
			if (getIntent().hasExtra("Id")) {
				strId = getIntent().getStringExtra("Id");
			}
			if (getIntent().hasExtra("TrnType")) {
				TrnType = getIntent().getStringExtra("TrnType");
			}

			Brand = "";
			Model = "";
			Made = "";
			SearchText = "";

			lvProduct.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapter, View v,
						int Pos, long Id) {
					// TODO Auto-generated method stub
					try {
						Bundle b = new Bundle();
						b.putString("Code", CustCode);
						b.putString("CName", CName);
						b.putString("Id", "0");
						b.putString("TrnType", TrnType);
						b.putString("Product", lstProducts.get(Pos).Product);
						Intent _intent = new Intent();
						_intent.putExtras(b);
						_intent.setClass(FrequentProduct.this,
								TrnProductDetails.class);
						startActivityForResult(_intent,
								PRODUCT_DETAILS_ACTIVITY);
					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			});
			lstProducts = ObjDBHelper.GetFrequentList(CustCode, "");

			productListAdapter = new FrequentListAdapter(FrequentProduct.this,
					lstProducts);
			lvProduct.setAdapter(productListAdapter);

			edtSearchProduct = (EditText) findViewById(R.id.edtSearchProduct);
			btnDelSearch = (Button) findViewById(R.id.btn_del_search);
			btnDelSearch.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Brand = "";
					Model = "";
					Made = "";
					edtSearchProduct.setText("");
					/*
					 * productListAdapter = new FrequentListAdapter(
					 * FrequentProduct.this, lstProducts);
					 * productListAdapter.notifyDataSetChanged();
					 * lvProduct.setAdapter(productListAdapter);
					 */
				}
			});

			btnSearhAdvanced = (LinearLayout) findViewById(R.id.btnSearhAdvanced);
			btnSearhAdvanced.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Bundle b = new Bundle();
					b.putString("Brand", Brand);
					b.putString("Model", Model);
					b.putString("Made", Made);
					Intent _intent = new Intent();
					_intent.putExtras(b);
					_intent.setClass(FrequentProduct.this,
							ProductAdvancedSearch.class);
					startActivityForResult(_intent, PRODUCT_SEARCH_ACTIVITY);

				}
			});

			edtSearchProduct.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					try {
						// lstProducts =
						// ObjDBHelper.GetFrequentList(CustCode,s.toString());
						String text = s.toString().toLowerCase(
								Locale.getDefault());
						SearchText = text;
						productListAdapter.filterByBMM(Brand, Model, Made,
								SearchText);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// lvProduct.setAdapter(new FrequentListAdapter(
					// FrequentProduct.this, lstProducts));

				}
			});

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void onBackPressed() {
		OrderTab parent = (OrderTab) getParent();
		parent.getTabHost().setCurrentTab(parent.previous_tab);
	}

	@SuppressLint("NewApi")
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case PRODUCT_DETAILS_ACTIVITY:
			if (resultCode == RESULT_OK) {
				Bundle bundle = data.getExtras();
				String status = bundle.getString("status");
				if (status.equalsIgnoreCase("done")) {
					OrderTab parent = (OrderTab) getParent();
					parent.UpdateDetail();
					Toast.makeText(FrequentProduct.this,
							"Product has been added successfully.",
							Toast.LENGTH_SHORT).show();
				}
			} else if (resultCode == RESULT_CANCELED) {

			}
			break;
		case PRODUCT_SEARCH_ACTIVITY:
			if (resultCode == RESULT_OK) {
				Bundle bundle = data.getExtras();
				String status = bundle.getString("status");
				if (status.equalsIgnoreCase("done")) {
					Brand = bundle.getString("Brand");
					Model = bundle.getString("Model");
					Made = bundle.getString("Made");
					productListAdapter.filterByBMM(Brand, Model, Made,
							SearchText);
				}

			} else if (resultCode == RESULT_CANCELED) {

			}
			break;
		}

	}

}